package com.spider.silence.worker;

import com.spider.silence.abstarct.downloader.AbstractCommonDownLoader;
import com.spider.silence.proxy.ProxyPool;
import com.spider.silence.task.WyzxModuleTask;
import com.spider.silence.task.WyzxTask;
import com.spider.silence.task.manager.CommonTaskManager;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.net.URLDecoder;

/**
 * @author Administrator
 * @description 描述
 * @time 2017/4/19 0019
 */
public class WyzxModuleWorker extends AbstractCommonDownLoader implements Runnable{
    private Logger logger = Logger.getLogger(WyzxModuleWorker.class);


    @Override
    public void run() {
        int page = 0;
        while(true&&!Thread.interrupted()){
            try {
                WyzxModuleTask moduleTask = (WyzxModuleTask) CommonTaskManager.takeTask("WyzxModule", 1000);

                if(null!=moduleTask){
                    String sub_forum_url = moduleTask.getModule_url();
                    String class_id = moduleTask.getClassId();
                    String channel = moduleTask.getChannel();
                    Document document = null;
                    try {
                        document = get(sub_forum_url);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                        document = get(sub_forum_url, ProxyPool.proxy());
                    }

                    Elements pageElements = document.select(".g-bd").select(".g-mn");

                    Elements content_urls = pageElements.select(".g-mnc").select(".m-pt");

                    for(int j = 0;j<content_urls.size();j++){

                        String content_url = content_urls.get(j).select("a").get(0).attr("href");
                        if(content_url.equals(sub_forum_url)){
                            content_url = content_urls.get(j).select("a").get(1).attr("href");
                        }

                        String regex = "http:(.+?)www\\.wyzxwk\\.com.*?";
                        if(content_url.matches(regex)){
                            WyzxTask task = new WyzxTask(URLDecoder.decode(content_url, "UTF-8"), channel, class_id);

                            CommonTaskManager.addTask("Wyzx", task, 1000);
                            System.out.println(content_url);
                        }
                        if(content_url.indexOf("qzone")<0&&content_url.indexOf("weixin")<0) {

                        }
                    }

                    String pageInfo = pageElements.select(".m-pages").select("a").get(pageElements.select(".m-pages").select("a").size()-2).text();

                    if(pageInfo!=null&&!"".equals(pageInfo)) {
                        page = Integer.parseInt(pageInfo);
                    }

                   /* if(page>=2){
                        for(int m = 2;m<=page;m++){
                            String new_url = sub_forum_url+"/index_"+m+".html";
                            Document document1 = null;

                            try {
                                document1 = get(new_url);
                            } catch (Exception e) {
                                logger.error(e.getMessage(), e);
                                document1 = get(new_url, ProxyPool.proxy());
                            }

                            Elements pageElements1 = document1.select(".g-bd").select(".g-mn");

                            Elements content_urls1 = pageElements1.select(".g-mnc").select(".m-pt");

                            for(int j = 0;j<content_urls1.size();j++){
                                String content_url = content_urls1.get(j).select("a").get(0).attr("href");
                                if(content_url.equals(new_url)||content_url.equals(sub_forum_url)){
                                    content_url = content_urls.get(j).select("a").get(1).attr("href");
                                }
                                String regex = "http:(.+?)www\\.wyzxwk\\.com.*";
                                if(content_url.matches(regex)){
                                    WyzxTask task = new WyzxTask(URLDecoder.decode(content_url, "UTF-8"), channel, class_id);

                                    CommonTaskManager.addTask("Wyzx", task, 1000);
                                    System.out.println(content_url);
                                }
                            }

                        }
                    }*/
                }
            }catch (Exception e){
                logger.error(e.getMessage(),e);
                e.printStackTrace();
            }

        }
    }
}
